package cn.cqie.attendance.common.dao;

import cn.cqie.attendance.common.pojo.Student;
import cn.cqie.attendance.common.pojo.Teacher;
import cn.cqie.attendance.common.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface LoginMapper extends BaseMapper<User> {

    /**
     * 根据学号查学生
     * @param stuNumber
     * @return
     */
    @Select("select * from student\n" +
            " where stu_number = #{stuNumber}")
    Student queryStudent(@Param("stuNumber") String stuNumber);

    /**
     * 根据工号查教师
     * @param jobNumber
     * @return
     */
    @Select("select * from teacher\n" +
            "where job_number = #{jobNumber}")
    Teacher queryTeacher(@Param("jobNumber") String jobNumber);

    /**
     * 增加登陆次数
     * @param id
     * @param role
     * @return
     */
    @Update({"<script>","update <choose>\n" +
            "<when test='role == \"student\"'>student</when>\n" +
            "<when test='role == \"teacher\"'>teacher</when>\n" +
            "<when test='role == \"admin\"'>teacher</when>\n" +
            "<otherwise></otherwise></choose>\n" +
            "set login_times = login_times + 1\n" +
            "where id = #{userId}","</script>"})
    int loginOnce(@Param("role") String role, @Param("userId") int id);

    /**
     * 修改密码
     * @param id
     * @param password
     * @param role
     * @return
     */
    @Update({"<script>","update <choose>\n" +
            "<when test='role == \"student\"'>student</when>\n" +
            "<when test='role == \"teacher\"'>teacher</when>\n" +
            "<when test='role == \"admin\"'>teacher</when>\n" +
            "<otherwise></otherwise></choose>\n" +
            "set password = #{newPassword}\n" +
            "where id = #{userId}","</script>"})
    int changePassword(@Param("userId") int id,
                       @Param("newPassword") String password,
                       @Param("role") String role);
}
